package org.clever.session.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.clever.common.model.response.ErrorResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.SessionRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(description = "root")
@RestController
@Slf4j
public class RootController {

    @Autowired
    private SessionRepository sessionRepository;

    @ApiOperation("echo")
    @GetMapping("/ok")
    public String ok(HttpServletRequest request) {
        HttpSession session = request.getSession();
        log.info("==={}", sessionRepository.toString());
        log.info("============{}", session.getAttribute("string"));
        log.info("============{}", session.getAttribute("int"));
        log.info("============{}", session.getAttribute("double"));
        log.info("============{}", session.getAttribute("boolean"));
        log.info("============{}", session.getAttribute("map"));
        log.info("============{}", session.getAttribute("errorResponse"));

        session.setAttribute("string", "123456");
        session.setAttribute("int", 123456);
        session.setAttribute("double", 123456.0123);
        session.setAttribute("boolean", true);

        Map<String, Object> map = new HashMap<>();
        map.put("string", "123456");
        map.put("int", 123456);
        map.put("double", 123456.0123);
        map.put("boolean", true);
        session.setAttribute("map", map);

        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setTimestamp(new Date());
        errorResponse.setStatus(500);
        errorResponse.setError("错误");
        session.setAttribute("errorResponse", errorResponse);
        return session.getId();
    }
}
